352B - Jeff and Periods - CodeForces Solution


implementation sortings *1300

Please click on ads to support us..

Python Code:

n=int(input()) ; arr=[int(i) for i in input().split()] ; dic=dict() ; c=0
for i in range(n):
    x=arr[i]
    if x not in dic: dic[x]=[i]
    else:
        dic[x].append(i)


arr2=[]
x=list(dic.keys())
x.sort()
for i in x:
    z=dic[i]
    if len(z)==1: arr2.append([i,0])
    else:
        d=z[1]-z[0]
        for j in range(2,len(z)):
            if z[j]-z[j-1]!=d:
                break
        else:arr2.append([i,d])
            

print(len(arr2))
for i in arr2: 
    print(*i)           
            

C++ Code:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

ll m = 1000000007;

void solve() {
    map<int,vector<int>> indexes;
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        indexes[a[i]].push_back(i+1);
    }
    int cnt = 0;
    vector<vector<int>> ans;
    for(auto i = indexes.begin(); i !=indexes.end(); i++) {
        vector<int> temp = i->second;
        if(temp.size() == 1) {
            cnt++;
            ans.push_back({i->first, 0});
            continue;
        }
        int diff = temp[1] - temp[0];
        int flag = 0;
        for(int j = 1; j < temp.size(); j++) {
            if(temp[j] - temp[j-1] != diff) {
                flag = 1;
                break;
            }
        }
        if(!flag) {
            cnt++;
            ans.push_back({i->first, diff});
        }
    }
    cout << cnt << endl;
    for(int i = 0; i < ans.size(); i++)
        cout << ans[i][0] << " " << ans[i][1] << endl;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++)
    {
        // cout << "Case #" << t << ": ";
        solve();
    }
}



Comments

Submit
0 Comments
More Questions

479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game